package com.click369.mianshiqingbao.service.impl;

import com.click369.mianshiqingbao.mapper.AnswerBeanMapper;
import com.click369.mianshiqingbao.pojo.AnswerBean;
import com.click369.mianshiqingbao.pojo.AnswerBeanExample;
import com.click369.mianshiqingbao.pojo.AnswerBeanExample.Criteria;
import com.click369.mianshiqingbao.pojo.entity.PageResult;
import com.click369.mianshiqingbao.service.AnswerService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 题目答案信息的业务访问接口实现类
 */
@Service
public class AnswerServiceImpl implements AnswerService{
    @Autowired
    private AnswerBeanMapper answerBeanMapper;
    /**
     * 条件分页查询题目答案信息列表
     * @return 分页查询信息的封装类对象
     */
    @Override
    public PageResult findPage(AnswerBean answerbean, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<AnswerBean> page=(Page<AnswerBean>)answerBeanMapper.selectByExample(null);
        return new PageResult(page.getTotal(),page.getResult());
    }
    /**
     * 增加题目答案信息
     * @param answerbean 题目答案信息对象
     */
    @Override
    public void add(AnswerBean answerbean) {
        answerBeanMapper.insert(answerbean);
    }
    /**
     * 根据ID获取题目答案信息对象
     * @param id 题目答案信息的id
     * @return 题目答案信息对象
     */
    @Override
    public AnswerBean findOne(Long id) {
        return answerBeanMapper.selectByPrimaryKey(id);
    }
    /**
     * 修改题目答案信息
     * @param answerbean 题目答案信息对象
     */
    @Override
    public void update(AnswerBean answerbean) {
        answerBeanMapper.updateByPrimaryKey(answerbean);
    }
    /**
     * 批量删除题目答案信息
     * @param ids 批量删除题目答案信息的id集合
     */
    @Override
    public void delete(Long[] ids) {
        for(long id:ids){
            answerBeanMapper.deleteByPrimaryKey(id);
        }
    }
    /**
     * 根据题目Id获取答案对象
     * @param topicId 题目Id
     * @return 答案对象
     */
    @Override
    public AnswerBean getAnswerBean(Long topicId) {
        AnswerBeanExample example = new AnswerBeanExample();
        Criteria criteria = example.createCriteria();
        criteria.andTopicIdEqualTo(topicId);
        List<AnswerBean> answerBeanList = answerBeanMapper.selectByExample(example);
        if(answerBeanList.size()>0){
            return answerBeanList.get(0);
        }
        return null;
    }
}
